# 引用库函数
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit

# 需要拟合的数据组
x_data = np.array(
    [-23.27, -46.33, -46.7, -50.91, -48.7, -49.47, -54.77, -58.85, -55.28, -58.98, -65.38, -53.45, -62.33, -54.87,
     -56.18, -54.53
     ])
y_data = np.array([0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5])


# 需要拟合的函数
def func(rss, a, n):
    return np.power(10, (abs(rss) - a) / (10 * n))


# 得到返回的A，B值
a, n = curve_fit(func, x_data, y_data)[0]
# 数据点与原先的进行画图比较
plt.scatter(x_data, y_data, marker='o', label='real')
x = np.arange(-80, 0, 0.5)
y = func(x, a, n)
plt.plot(x, y, color='red', label='curve_fit')
plt.legend()
plt.title("a={}, n={}".format(a, n))
print("a={}, n={}".format(a, n))
plt.show()
